<template>
    <div>
        <hd-tab :tabs="tabs"></hd-tab>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>发布公告信息</span>
            </div>
            <el-form ref="form" :model="article" label-width="80px">
                <div v-if="article">
                    <div v-for="(field, index) in article.fields" :key="index">
                        <el-form-item :label="field.title" v-if="field.show">
                            <component v-bind:is="`field-${field.type}`" :field="field" :form="form" :alert="alert"></component>

                            <hd-error :name="field.name"/>
                        </el-form-item>
                    </div>
                </div>

            </el-form>
            <el-button type="success" @click="onSubmit">保存提交</el-button>
        </el-card>
    </div>
</template>

<script>
import tabs from './tabs'
import fields from '../field/fields'

const form = {
    title: '',
    content: '',
    preview: '',
    url: '',
    source: '',
    click: 0,
    keywords: null,
    author: null,
}
export default {
    route:false,
    props: ['id'],
    data() {
        return {
            tabs,
            form,
            article: {},
            fields,
            alert: {title: "正方形图片最好", type: 'success'}

        }
    },

    async created() {
        //获取当前模型
        this.article = await this.axios.get(`admin/article/1`)
        //获取当前notice的数据
        if (this.id) {
            this.form = await this.axios.get(`admin/article/1/notice/${this.id}`)
        }
    },
    methods: {
        async onSubmit() {
            this.form = Object.assign(this.form, {article_id: 1})
            const url = this.id ? `admin/article/1/notice/${this.id}` : `admin/article/1/notice`;
            await this.axios[this.id ? 'put' : 'post'](url, this.form)
            this.$router.push({name: 'admin.notice.index'})
        }
    }
}
</script>

<style scoped>

</style>
